; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
; RUN: opt -passes=globalopt -S %s | FileCheck %s

%struct.widget = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@global = internal global %struct.widget zeroinitializer

;.
; CHECK: @[[A:[a-zA-Z0-9_$"\\.-]+]] = global i8 0, align 4
;.
define internal void @read_struct() {
; CHECK-LABEL: @read_struct(
; CHECK-NEXT:    ret void
;
  %tmp = load ptr, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 16), align 8
  ret void
}

define void @write_struct() {
; CHECK-LABEL: @write_struct(
; CHECK-NEXT:    tail call fastcc void @read_struct()
; CHECK-NEXT:    ret void
;
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 0), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 1), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 2), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 3), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 4), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 5), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 6), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 7), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 8), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 9), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 10), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 11), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 12), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 13), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 14), align 8
  store ptr null, ptr getelementptr inbounds (%struct.widget, ptr @global, i64 0, i32 15), align 8
  tail call void @read_struct()
  ret void
}


%struct.with.array = type { [100 x i64], i64 }

@global.array_in_struct = internal global %struct.with.array zeroinitializer

define internal void @read_non_array_field() {
; CHECK-LABEL: @read_non_array_field(
; CHECK-NEXT:    ret void
;
  %tmp = load i64, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 1), align 8
  ret void
}

define void @store_to_struct_array() {
; CHECK-LABEL: @store_to_struct_array(
; CHECK-NEXT:    tail call fastcc void @read_non_array_field()
; CHECK-NEXT:    ret void
;
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 0), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 1), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 2), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 3), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 4), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 5), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 6), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 7), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 8), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 9), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 10), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 11), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 12), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 13), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 14), align 8
  store i64 0, ptr getelementptr inbounds (%struct.with.array, ptr @global.array_in_struct, i64 0, i32 0, i32 15), align 8
  tail call void @read_non_array_field()
  ret void
}

@global.array = internal global [100 x i64] zeroinitializer

define internal void @read_array() {
; CHECK-LABEL: @read_array(
; CHECK-NEXT:    ret void
;
  %tmp = load i64, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 99), align 8
  ret void
}

define void @store_to_array() {
; CHECK-LABEL: @store_to_array(
; CHECK-NEXT:    tail call fastcc void @read_array()
; CHECK-NEXT:    ret void
;
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 0), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 1), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 2), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 3), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 4), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 5), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 6), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 7), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 8), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 9), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 10), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 11), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 12), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 13), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 14), align 8
  store i64 0, ptr getelementptr inbounds ([100 x i64], ptr @global.array, i64 0, i32 15), align 8
  tail call void @read_array()
  ret void
}


%struct.20i8 = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8 }
@a = global i8 0, align 4
@b = internal global  %struct.20i8 { i8 4, i8 4, i8 4, i8 5, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 5, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4 }

define void @test_single_write_to_global_b() {
; CHECK-LABEL: @test_single_write_to_global_b(
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 5, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 5, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    store volatile i8 4, ptr @a, align 4
; CHECK-NEXT:    ret void
;
  store i8 0, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 1), align 1
  %i3 = load i8, ptr @b, align 16
  store volatile i8 %i3, ptr @a, align 4
  %i4 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 2), align 2
  store volatile i8 %i4, ptr @a, align 4
  %i5 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 3), align 1
  store volatile i8 %i5, ptr @a, align 4
  %i6 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 4), align 4
  store volatile i8 %i6, ptr @a, align 4
  %i7 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 5), align 1
  store volatile i8 %i7, ptr @a, align 4
  %i8 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 7), align 1
  store volatile i8 %i8, ptr @a, align 4
  %i9 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 8), align 8
  store volatile i8 %i9, ptr @a, align 4
  %i10 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 9), align 1
  store volatile i8 %i10, ptr @a, align 4
  %i11 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 10), align 2
  store volatile i8 %i11, ptr @a, align 4
  %i12 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 11), align 1
  store volatile i8 %i12, ptr @a, align 4
  %i13 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 12), align 4
  store volatile i8 %i13, ptr @a, align 4
  %i14 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 13), align 1
  store volatile i8 %i14, ptr @a, align 4
  %i15 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 14), align 2
  store volatile i8 %i15, ptr @a, align 4
  %i16 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 15), align 1
  store volatile i8 %i16, ptr @a, align 4
  %i17 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 16), align 16
  store volatile i8 %i17, ptr @a, align 4
  %i18 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 17), align 1
  store volatile i8 %i18, ptr @a, align 4
  %i19 = load i8, ptr getelementptr inbounds (%struct.20i8, ptr @b, i64 0, i32 18), align 2
  store volatile i8 %i19, ptr @a, align 4
  ret void
}
